package com.fdd.furns.web;

import com.fdd.furns.entity.Member;
import com.fdd.furns.service.MemberService;
import com.fdd.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/registerServlet")
public class RegisterServlet extends HttpServlet {
    private final MemberService memberService = new MemberServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("doPost...");
        //接受用户注册的信息，这里一定要看前端的页面

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");

        //判断是否存在，存在就不能注册
        if (!memberService.isExistUsername(username)) {
            Member member = new Member(null, username, password, email);
            if (memberService.registerMember(member)) {
                //注册成功
                req.getRequestDispatcher("/views/member/register_ok.html").forward(req, resp);
            } else {
                //注册失败
                req.getRequestDispatcher("/views/member/register_fail.html").forward(req, resp);
            }

        }else{
            //用户名已经存在，返回登录页面
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }

    }
}
